﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Wicture.EPC.Common.Biz
{
    public class DateOperator
    {
        public void InterSection(List<FromToModel> list, out int from, out int to)
        {
            from = 0;
            to = 999999;
            foreach (var model in list)
            {
                if (model.From > from)
                {
                    from = model.From;
                }
                if (model.To < to)
                {
                    to = model.To;
                }
            }
        }

        public void Union(List<FromToModel> list, out int from, out int to)
        {
            from = list[0].From;
            to = list[0].To;
            foreach (var model in list)
            {
                if (model.From < from)
                {
                    from = model.From;
                }
                if (model.To > to)
                {
                    to = model.To;
                }
            }
        }

        public static string ReverseDate(string toDate)
        {
            if (string.IsNullOrEmpty(toDate))
            {
                return toDate;
            }
            return toDate.Substring(2, 2) + toDate.Substring(0, 2);
        }


        public static bool ComparerDate(int? fromDate, int? toDate, int proDateFrom, int proDateTo)
        {
            if (proDateFrom != 0)
            {
                proDateFrom = GetYearMonth(proDateFrom);
            }
            if (proDateTo != 0)
            {
                proDateTo = GetYearMonth(proDateTo);
            }
            else
            {
                proDateTo = 999999;
            }

            if (fromDate.HasValue && GetYearMonth(fromDate.Value) > proDateTo)
            {
                return false;
            }
            if (toDate.HasValue && GetYearMonth(toDate.Value) < proDateFrom)
            {
                return false;
            }
            return true;
        }

        public static int ComparerDate(int date1, int date2)
        {
            date1 = GetYearMonth(date1);
            date2 = GetYearMonth(date2);
            if (date1 == date2)
            {
                return 0;
            }
            else if (date1 < date2)
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }

        public static int GetYearMonth(int date)
        {
            if (date < 5000)
            {
                date += 200000;
            }
            else
            {
                date += 190000;
            }
            return date;
        }
        public static string GetYearMonthStr(int? date, char split = '/')
        {
            if (!date.HasValue)
            {
                return "";
            }
            if (date < 5000)
            {
                date += 200000;
            }
            else
            {
                date += 190000;
            }
            string dateStr = date.ToString();
            return dateStr.Substring(0, 4) + split + dateStr.Substring(4, 2);
        }

        public static bool CompareOriginalDate(int? fromDate, int? toDate, int proDateFrom, int proDateTo)
        {
            if (fromDate.HasValue && fromDate > proDateTo)
            {
                return false;
            }
            if (toDate.HasValue && toDate < proDateFrom)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="timeStamp">Unix时间戳格式</param>
        /// <returns>C#格式时间</returns>
        public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = new DateTime(1970, 1, 1);
            long lTime = long.Parse(timeStamp + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp(System.DateTime time)
        {
            long ts = ConvertDateTimeToInt(time);
            return ts.ToString();
        }
        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long ConvertDateTimeToInt(System.DateTime time)
        {
            DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;
        }
    }

    public class FromToModel
    {
        public int From { set; get; }
        public int To { set; get; }
    }
}
